<!DOCTYPE html>
<html xmlns:th="http://www.w3.org/1999/xhtml">

<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width">
    <title>配置设置</title>
    {include file="public/resource/layui_css" /}
    <link rel="stylesheet" href="/static/admin/css/style.css">
    <style>
        .layui-row {
            margin-bottom: 20px;
        }
        .layui-row .input-name {
            width: 180px;
        }
        .layui-row .input-value {
            width: 560px;
        }
        .layui-upload-drag {
            padding: 6px;
        }
    </style>
</head>

<body>
    <!-- 内容主体区域 -->
    <div class="my-form">
        <fieldset class="layui-elem-field layui-field-title">
            <legend>配置设置</legend>
        </fieldset>
        <form class="layui-form">
            {volist name='list' id='one'}
            <div class="layui-form layui-row layui-col-space16">
                <div class="layui-col-md3">
                    <input type="text" name="name[]" {eq name="one.is_default" value="1"}readonly="readonly"{/eq} placeholder="请输入字段名" value="{$one.name}" class="layui-input input-name">
                </div>
                <div class="layui-col-md9">
                    {eq name="one.type" value="1"}
                    <input type="text" name="value[]" placeholder="请输入字段值" value="{$one.value}" class="layui-input input-value">
                    {else/}
                    {eq name="one.type" value="3"}
                    <div class="layui-inline">
                        <div class="layui-upload-drag" id="uploadImage">
                            <i class="layui-icon"></i>
                            <p>点击上传，或将图片拖拽到此处</p>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <div class="{empty name='one.value'}layui-hide {/empty}upload-view" id="uploadView">
                            <img src="{$one.value}" id="logoImg">
                        </div>
                    </div>
                    <div class="layui-inline">
                        <button {empty name='one.value'}layui-hide {/empty} type="button" class="layui-btn{empty name='one.value'} layui-hide {/empty}" id="deleteLogo">删 除</button>
                    </div>
                    <input type="hidden" name="value[]" class="input-value" id="logo" value="{$one.value}">
                    {else/}
                    <textarea name="value[]" placeholder="请输入字段值" class="layui-textarea input-value">{$one.value}</textarea>
                    {/eq}
                    {/eq}
                </div>
            </div>
            {/volist}
            <div class="layui-form layui-row layui-col-space16">
                <div class="layui-col-md12">
                    <button type="button" class="layui-btn" lay-submit="" lay-filter="addSetting">保 存</button>
                </div>
            </div>
        </form>
    </div>
    {include file="public/resource/layui_js" /}
    <script>
        // JavaScript代码区域
        layui.use(['form', 'upload', 'element', 'laydate', 'layer'], function () {
            let $ = layui.jquery, form = layui.form, layer = layui.layer, upload = layui.upload;
            // 上传图片
            upload.render({
                elem: '#uploadImage'
                ,url: '/admin/upload/image?w=200&h=80' //此处配置上传接口
                ,accept: 'images' //图片文件
                ,field: 'uploadImage'
                , before: function () {
                    layer.load(); //上传loading
                }
                ,done: function(result){
                    layer.closeAll('loading'); //关闭loading
                    if (result.code === 0) {
                        layer.msg('图片上传成功');
                        $("#logo").val(result.data.url);
                        layui.$("#deleteLogo").removeClass('layui-hide');
                        layui.$('#uploadView').removeClass('layui-hide').find('img').attr('src', result.data.url);
                    } else {
                        layer.msg("图片上传失败")
                    }
                }
            });
            // 监听提交
            form.on('submit(addSetting)', function (data) {
                let inputNames = $("input[name='name[]']"), inputValues = $(".input-value");
                let names = [], values  = [];
                for (i=0; i < inputNames.length; i++) {
                    names[i] = inputNames[i].value;
                }
                for (i=0; i < inputValues.length; i++) {
                    values[i] = inputValues[i].value;
                }
                let newData = {names: names, values:values}
                $.ajax({
                    type: 'post',
                    url: '/admin/setting/save',
                    dataType: 'json',
                    contentType: "application/json; charset=utf-8",
                    data: JSON.stringify(newData),
                    success: function (result) {
                        if (result.code === 0) {
                            layer.msg('添加成功。');
                            setTimeout(function () {
                                location.href="/admin/setting";
                            }, 1000);
                        } else {
                            layer.msg(result.msg);
                        }
                    }
                });
                return false;
            });
            layui.$("#deleteLogo").click(function (){
                layui.$(this).addClass('layui-hide')
                layui.$('#uploadView').addClass('layui-hide').find('img').attr('src', "");
                $("#logo").val('');
            });
        });
        function deleteTag(obj, tag) {
            obj.parentElement.parentElement.remove();
            tags.splice(tags.indexOf(tag), 1);
        }
    </script>
</body>

</html>